package com.julang.check.controller;

import com.google.gson.Gson;
import com.julang.check.bean.*;
import com.julang.check.api.mapper.UserMapper;
import com.julang.check.translate.baidu.BaiduTranslator;
import com.julang.check.utils.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

import static com.julang.check.utils.CommonData.*;
import static com.julang.check.utils.LanguageUtil.getJifenOfLanguage;
import static com.julang.check.utils.MessageInfo.*;

import com.julang.check.bean.CommonEntity.*;
/**
 * @author 赵自强
 * */
@RestController
@RequestMapping(value = "/api/trans")
public class TranslateController extends BaseController{

	@Resource
	private UserMapper userMapper;

	@GetMapping("/alllanguage")
	@ResponseBody
	public Result<List<Language>> alllanguage() {
		return Result.buildSuccess(SUCCESSINFO, CommonData.LANGUAGES);
	}

	@GetMapping("/translateMode")
	@ResponseBody
	public Result<List<Language>> getTranslageMode() {
		return Result.buildSuccess(SUCCESSINFO, SingnalData.TranslateMode);
	}

	@PostMapping("/translateMode")
	@ResponseBody
	public Result setTranslageMode(@RequestBody String body) {
		SingnalData.TranslateMode.clear();
		String[] ids = body.split("-");
		for (int i = 0; i < ids.length; i++) {
			int index = Integer.valueOf(ids[i]);
			SingnalData.TranslateMode.add(CommonData.LANGUAGES.get(index));
		}
		return new Result(true, "", SingnalData.TranslateMode);
	}

	@PostMapping("/trans")
	@ResponseBody
	public Result<TranslateResult.TransResult[]> trans(@RequestBody RequestEntity.TransLateRequest body){
		BaiduTranslator api = new BaiduTranslator(APP_ID, SECURITY_KEY);
		String s = api.getTransResult(body.getQuery(), body.getFrom(), body.getTo());
		if (s.contains(LF)) {
			System.out.println("you");
		}
		Result<TranslateResult.TransResult[]> result = new Result<>();
		if (s.startsWith(ERRORCODESTART)) {
			result.setResult(false);
			result.setMessage(s);
		} else {
			TranslateResult translateResult = new Gson().fromJson(s, TranslateResult.class);
			result.setResult(true);
			result.setData(translateResult.getTransResult());
		}
		return result;
	}

	@PostMapping("/repeat_reduction")
	@ResponseBody
	public Result<TranslateResult.TransResult[]> repeatReduction(@RequestBody RequestEntity.RepeatReductionBody body,
	                                                             HttpServletRequest request) throws IOException {
		String query = body.getQuery().replaceAll("\\s*", "");
		Result result;
		String userId = this.getUserId();
		User user = userMapper.selectById(userId);
		if (user == null) {
			return Result.buildError(USERNOTEXIST, null);
		}
		if (body.getJifen() != getJifenOfLanguage(body.getQuery())) {
			return Result.buildError(JIFENCALCULATEERROR, null);
		}
		if (user.getJifen() < body.getJifen()) {
			return Result.buildError(JIFENNOTENOUGH, null);
		}
		if (body.getType() == ONEOFNUMBER) {
			result = LanguageUtil.getBaiduRepeatReductionResult(query);
		} else if (body.getType() == TWOOFNUMBER) {
			result = LanguageUtil.getGooleReductionResult(query);
		} else if (body.getType() == THREEOFNUMBER) {
			result = LanguageUtil.getJinShanReductionResult(query);
		} else {
			result = new Result();
			result.setResult(false);
		}
		if (result.isResult()) {
			user.setJifen(user.getJifen() - body.getJifen());
			userMapper.updateById(user);
		}
		return result;


	}

	public static void main(String[] args) {
		String s =
				"德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚德玛西亚";
		System.out.println(s.length());
		System.out.println(getJifenOfLanguage(s));
	}
}
